import { getToken, removeToken, setToken } from '@/utils/token'
import { login } from '@/api/user'

const state = {
  token: getToken()
}
const mutations = {
  SET_TOKEN(state, token) {
    state.token = token
    setToken(token)
  },
  REMOVE_TOKEN(state) {
    state.token = ''
    removeToken()
  }
}
const actions = {
  async login(context, form) {
    const res = await login(form) // 调用封装好的登录接口
    context.commit('SET_TOKEN', res.data.token) // 调用mutations 存储token&持久化
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
